package quick.portal.fragment.notice;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;
import quick.communication.AsyncHttpResponseHandler;
import quick.portal.R;
import quick.portal.activity.BaseEditActivity;
import quick.portal.data.APIResult;
import quick.portal.data.NoticeInfo;
import quick.portal.service.BaseService;
import quick.view.webview.QuickWebView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.widget.TextView;

public class NoticeDetailActivity extends BaseEditActivity {

	private TextView tvName;
	private TextView tvCreateUser;
	private TextView tvApproveUser;
	private QuickWebView wvContent;
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);

        this.setContentView(R.layout.notice_detail_layout);        
        this.setBackButton();
        
        tvName = (TextView)this.findViewById(R.id.name);
        tvCreateUser = (TextView)this.findViewById(R.id.createUserName);
        tvApproveUser = (TextView)this.findViewById(R.id.approveUserName);
        
        wvContent = (QuickWebView)this.findViewById(R.id.Content);        
        WebSettings websettings = wvContent.getSettings();
        websettings.setJavaScriptEnabled(true);
        websettings.setBuiltInZoomControls(true);
        
        Intent intent = this.getIntent();
        long id = intent.getLongExtra("NoticeInfoId",0);
        boolean isRead = intent.getBooleanExtra("IsRead",true);
        
        Map<String,String> params = new HashMap<String,String>();
        params.put("NoticeInfoId", String.valueOf(id));
        params.put("IsRead", String.valueOf(isRead));
        
        new BaseService().GetObject(params,"GetNoticeInfo",new AsyncHttpResponseHandler() {
        	@Override
            public void onSuccess(String body) {

                APIResult result = JSON.parseObject(body, APIResult.class);
    			
    			if(result.getReturnCode()<0)
	        	{
    				tvName.setText("获取数据失败："+result.getMessage());
					return;
	        	}
    			
    			NoticeInfo notice = JSON.parseObject(result.getBody(), NoticeInfo.class);
    			
    			tvName.setText(notice.getName());
    			tvCreateUser.setText(notice.getCreateUserName()+"  创建于  "+notice.getCreateTime());
    			tvApproveUser.setText(notice.getApproveUserName()+"  发布于  "+notice.getApproveTime());
    			wvContent.loadDataWithBaseURL(null,notice.getContent(), "text/html",  "utf-8", null);
        	}
        	
        	@Override
            public void onFailure(Request request, IOException e) {
        		tvName.setText("获取数据失败："+e.getMessage());
            }
        	
        });
        
	}
}
